
%plotting area strains for live cells 
figure
hold on 
for i=1:size(areas,2)
    area=areas(:,i);
    area = area(area~=0);
    area = [0; area]; 
    time = times(1:length(area));
    plot(time,area); 
end

%for single cells getting peaks  
area=areas(:,3);
area = area(area~=0);
area = [0; area]; 
time = times(1:length(area));

[up1,lo1] = envelope(area, 1, 'peak');
low= find(lo1==area);
high= find(up1==area); 
figure
plot(low, area(low), 'k*', 'linewidth', 5)
hold on
plot(high, area(high), 'r*', 'linewidth', 5)
plot(area)

%for all cells

vals = zeros(size(areas));
for i=1:size(areas,2)
    area=areas(:,i);
    area = area(area~=0);
    area = [0; area]; 
    [up1,lo1] = envelope(area, 1, 'peak');
    low= find(lo1==area);
    high= find(up1==area); 
    for j=1:length(low)
        vals(low(j),i)=1;
    end
    for j=1:length(high)
        vals(high(j),i)=2;
    end
end

amps = [];
amp_i = 1;

for i=1:size(vals,2)
    for j=1:length(vals)
        found = 0;
        if vals(j,i)~=0
            %find next nonzero value
            for k=j+1:length(vals)
                if vals(k,i)~=0 && found==0
                    if vals(k,i)~=vals(j,i)% if different, it's a high and low, find half the distance
                        amps(amp_i) = abs(areas(j,i)-areas(k,i))/2; 
                        amp_i = amp_i + 1; 
                        found =1; 
                    end
                end
            end
        end
    end
end

freq = [];
freq_i = 1; 
found = 0; 

for i=1:size(vals,2)
    for j=1:length(vals)
        found = 0;
        if vals(j,i)~=0 
            %find next nonzero value
            for k=j+1:length(vals)
                if vals(k,i)~=0 && found==0
                    if vals(k,i)==vals(j,i) % want to find time between same reference points
                        freq(freq_i) = (times(k)-times(j)); % invert for frequency
                        freq_i = freq_i + 1; 
                        found = 1;
                    end
                end
            end
        end
    end
end

% get strains_all using simulated data
% cut repeated strains off end before putting in prism 
plot(strains_all)

figure
hold on 
for i=1:size(strains_all,2)
    strain=strains_all(:,i);
    strain = strain(strain~=0);
    strain = [0; strain]; 
    plot(strain)
end

vals2 = zeros(size(strains_all));
for i=1:size(strains_all,2)
    strain=strains_all(:,i);
    [up1,lo1] = envelope(strain, 1, 'peak');
    low= find(lo1==strain);
    high= find(up1==strain); 
    for j=1:length(low)
        vals2(low(j),i)=1;
    end
    for j=1:length(high)
        vals2(high(j),i)=2;
    end
end

amps2 = [];
amp_i = 1;

for i=1:size(strains_all,2)
    for j=1:length(strains_all)
        found = 0;
        if vals2(j,i)~=0
            %find next nonzero value
            for k=j+1:length(vals2)
                if vals2(k,i)~=0 && found==0
                    if vals2(k,i)~=vals2(j,i)% if different, it's a high and low, find half the distance
                        amps2(amp_i) = abs(strains_all(j,i)-strains_all(k,i))/2; 
                        amp_i = amp_i + 1; 
                        found =1; 
                    end
                end
            end
        end
    end
end

freq2 = [];
freq_i = 1; 
found = 0; 

for i=1:size(vals2,2)
    for j=1:length(vals2)
        found = 0;
        if vals2(j,i)~=0 
            %find next nonzero value
            for k=j+1:length(vals2)
                if vals2(k,i)~=0 && found==0
                    if vals2(k,i)==vals2(j,i) % want to find time between same reference points
                        freq2(freq_i) = k-j; % invert for frequency
                        freq_i = freq_i + 1; 
                        found = 1;
                    end
                end
            end
        end
    end
end

figure
hist(amps)

figure
hist(amps2)

figure
hist(freq)

figure
hist(freq2)

figure
hist(1./freq)

figure 
hist(1./freq2)


ranksum(amps, amps2)
ranksum(freq, freq2)
